package cn.elwy.rcp.kit.intro;

import java.util.concurrent.TimeUnit;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(800, 600)); // 设置窗口初始化大小
		configurer.setTitle("ElwyKit"); // 设置窗口标题
		// configurer.setShellStyle(SWT.NO_TRIM); // 设置窗口样式

		configurer.setShowCoolBar(true); // 显示工具栏
		configurer.setShowMenuBar(true); // 显示菜单栏
		configurer.setShowStatusLine(true); // 显示状态栏
		configurer.setShowProgressIndicator(true); // 显示进度指示器
		configurer.setShowPerspectiveBar(true); // 显示透视图按钮

	}

	/**
	 * Here we check the menu bar and remove all items that aren't ours..
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowCreate()
	 */
	public void postWindowCreate() {
		super.postWindowCreate();
		IWorkbenchWindow window = getWindowConfigurer().getWindow();
		final Shell shell = window.getShell();
		shell.setAlpha(0);
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				int alpha = shell.getAlpha();
				while (shell.getAlpha() < 255) {
					shell.setAlpha(alpha++);
					try {
						TimeUnit.MILLISECONDS.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});

		shellCentre(shell); // 设置窗口自动居中，必需写在设置窗口最大化之前
		shell.setMaximized(true); // 设置窗口最大化
		shell.setMinimumSize(800, 600); // 设置窗口最小值

		handlerWindowElements();
	}

	public boolean preWindowShellClose() {
		boolean result = false;
		MessageBox msgBox = new MessageBox(new Shell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION);
		msgBox.setText("确认");
		msgBox.setMessage("确定要退出构建管理工具吗？");
		int open = msgBox.open();
		if (open == SWT.YES) {
			result = true;
		}
		return result;
	}

	/**
	 * 相对屏幕居中
	 * @param control 当前控件
	 */
	private void shellCentre(Control control) {
		Display display = Display.getCurrent();
		if (display == null) {
			display = Display.getDefault();
		}
		int width = display.getBounds().width;
		int height = display.getBounds().height;
		control.setLocation(width / 2 - control.getSize().x / 2, height / 2 - control.getSize().y / 2);
	}

	/**
	 * 处理Window要打开后要显示或隐藏的元素
	 */
	private void handlerWindowElements() {
		IWorkbenchPage page = getWindowConfigurer().getWindow().getActivePage();

		// page.hideView(page.findView(AppID.LOG_VIEW_ID));

		page.hideActionSet("org.eclipse.ui.WorkingSetActionSet");

		page.showActionSet("org.eclipse.ui.actionSet.keyBindings");

		IMenuManager menuBar = getWindowConfigurer().getActionBarConfigurer().getMenuManager();
		hideMenuItem(menuBar, "help", "org.eclipse.ui.actions.showKeyAssistHandler");

		// refresh menubar
		menuBar.updateAll(true);
	}

	/**
	 * Hide a menu item from the menu bar
	 * @param menuBar
	 * @param menupath
	 * @param id
	 */
	private void hideMenuItem(IMenuManager menuBar, String menupath, String id) {
		IMenuManager menu = menuBar.findMenuUsingPath(menupath);
		if (menu == null) {
			return;
		}
		IContributionItem item = menu.findUsingPath(id);
		if (item != null) {
			item.setVisible(false);
		}
	}

}
